package com.travel.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.dto.HotelOrderDto;
import com.travel.pojo.Order;
import com.travel.pojo.User;
import com.travel.service.OrderService;
import com.travel.util.JedisPoolUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("hotelOrder")
@Api(value = "酒店订单服务接口")
public class HotelOrderController {

    @Resource
    OrderService orderService;

    @PostMapping("queryHotelOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = true, dataType = "int"),
    })
    @ApiOperation(value = "查询酒店订单", notes = "查询酒店订单")
    public Page<Order> queryHotelOrder(@RequestBody Map<String,Object> params, HttpServletRequest request) {
        Object pageNum = params.get("pageNum");
        Object pageSize = params.get("pageSize");
        Object status = params.get("status");
        Integer userInfoFromRedis = getUserId(request);
        System.out.println(pageNum);
        System.out.println(pageSize);
        System.out.println(status);
        return orderService.queryHotelOrder(Integer.parseInt(pageNum.toString()), Integer.parseInt(pageSize.toString()),Integer.parseInt(status.toString()), userInfoFromRedis);
    }


    @PostMapping("generateHotelOrder")
    @ApiOperation(value = "生成订单", notes = "生成订单")
    public HashMap<String, String> generateHotelOrder(HotelOrderDto hotelOrderDto, HttpServletRequest request) {

        Integer userId = getUserId(request);
        HashMap<String, String> result = orderService.generateHotelOrder(hotelOrderDto, userId);
        return result;

    }

    private static Integer getUserId(HttpServletRequest request) {
        String authorizationHeader = request.getHeader("token");
        System.out.println(authorizationHeader);
        System.out.println(authorizationHeader);
        System.out.println(authorizationHeader);
        System.out.println(authorizationHeader);
        System.out.println(authorizationHeader);
        System.out.println(authorizationHeader);
        if (authorizationHeader != null) {

            Jedis jedis = JedisPoolUtil.getJedisPoolInstance().getResource();
            String s = jedis.get(authorizationHeader);
            User user = JSONObject.parseObject(s, User.class);
            System.out.println(s);
            System.out.println(user);
            Integer userid = user.getUserid();
            return userid;
        }

        throw new RuntimeException("No token cookie found");
    }


}
